package com.cg.controller;

import com.cg.dao.NoticeDepartmentMapper;
import com.cg.dto.*;
import com.cg.entity.NoticeDepartment;
import com.cg.service.NoticeService;
import com.cg.vo.NoticeVo;
import com.cg.vo.QueryNoticeByNoticeIdVo;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * (Notice)表控制层
 *
 * @author chengong
 * @since 2023-12-16 09:17:04
 */
@RestController
@RequestMapping("/notice")
public class NoticeController {
    /**
     * 服务对象
     */
    @Resource
    private NoticeService noticeService;

    @Resource
    private NoticeDepartmentMapper noticeDepartmentMapper;

    /**
     * 查询公告信息
     */
    @PostMapping("/noticeList")
    Result<Map<String,Object>> noticeList(@RequestBody NoticePageDto noticePageDto){
        PageInfo<NoticeVo> noticeVoPageInfo = noticeService.noticeList(noticePageDto);
        Map<String,Object> map = new HashMap<>();
        map.put("pageInfo",noticeVoPageInfo);
        return ResultUtils.returnDataSuccess(map);
    }

    /**
     * 新增公告信息
     */
    @PostMapping("/createNotice")
    public Result createNotice(@RequestBody NoticeDepartmentDto noticeDepartmentDto){
        Integer count = noticeService.createNotice(noticeDepartmentDto);
        if(count <= 0){
            //添加失败
            return ResultUtils.returnResult(ResultEnum.FAIL_LOGIN);
        }
        return ResultUtils.returnSuccess();
    }


    /**
     * 根据部门id查询部门详细信息
     */
    @PostMapping("/queryNotice")
    public Result<QueryNoticeByNoticeIdVo> queryNotice(@RequestParam("id") Integer id){
        QueryNoticeByNoticeIdVo queryNoticeByNoticeIdVo = noticeService.queryNotice(id);
        if(queryNoticeByNoticeIdVo == null){
            return ResultUtils.returnFail("部门信息不存在");
        }
        return ResultUtils.returnDataSuccess(queryNoticeByNoticeIdVo);
    }


    /**
     * 删除公告信息
     */
    @PostMapping("/deleteNoticeById")
    public Result deleteNoticeById(@RequestParam("noticeId") Integer noticeId){
        //先查询关联表有没有信息
        List<NoticeDepartment> noticeDepartments = noticeDepartmentMapper.queryNoticeDepartment(noticeId);
        if(noticeDepartments == null || noticeDepartments.size() == 0){
            noticeService.deleteNoticeById(noticeId);
            return ResultUtils.returnSuccess();

        }
        //表里有数据
        return ResultUtils.returnResult(ResultEnum.FAIL_DELETE_ROLE);

    }


    /**
     * 修改公告信息
     */
    @PostMapping("/setNotice")
    public Result setNotice(@RequestBody NoticeDepartmentDto noticeDepartmentDto){
        Integer count = noticeService.setNotice(noticeDepartmentDto);
        if(count <= 0){
            return ResultUtils.returnFail("修改失败");
        }
        return ResultUtils.returnSuccess();
    }
}

